package cn.edu.xmu.oomall.order.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.order.mapper.OrderPoMapper;
import cn.edu.xmu.oomall.order.mapper.PaymentPoMapper;
import cn.edu.xmu.oomall.order.model.bo.Payment;
import cn.edu.xmu.oomall.order.model.po.OrderPo;
import cn.edu.xmu.oomall.order.model.po.PaymentPo;
import cn.edu.xmu.oomall.order.model.po.PaymentPoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

@Repository
public class PaymentDao {
    @Autowired
    private OrderPoMapper orderPoMapper;
    @Autowired
    private PaymentPoMapper paymentPoMapper;

    public ReturnObject getPaymentById(Long id, Long userId) {
        try {
            OrderPo orderPo = orderPoMapper.selectByPrimaryKey(id);
            if (!orderPo.getCustomerId().equals(userId)) {
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            PaymentPoExample example = new PaymentPoExample();
            PaymentPoExample.Criteria criteria = example.createCriteria();
            criteria.andDocumentIdEqualTo(orderPo.getOrderSn());
            List<PaymentPo> paymentPos = paymentPoMapper.selectByExample(example);
            return new ReturnObject(paymentPos);

        }catch (Exception e) {
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject getByDocumentId(String documentId) {
        try {
            PaymentPoExample example = new PaymentPoExample();
            PaymentPoExample.Criteria criteria1 = example.createCriteria();
            criteria1.andDocumentIdEqualTo(documentId);
            List<PaymentPo> paymentPos = paymentPoMapper.selectByExample(example);
            Payment payment = cloneVo(paymentPos.get(0), Payment.class);
            return new ReturnObject(payment);
        }catch (Exception e) {
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

}
